/**
 * 区域排班 编辑页
 *
 * 作者 admin
 * 日期 2024-07-31 14:35:45
 */
layui.use(['jquery', 'febs', 'laydate', 'form', 'commonJS', 'validate','autocomplete'], function () {
    var $ = layui.$,
        febs = layui.febs,
        laydate = layui.laydate,
        layer = layui.layer,
        form = layui.form,
        $view = $('#baseAreaScheduling-edit'),
        $addWork = $view.find("#addWork"),
        validate = layui.validate,
        panelCount = 1;

    form.verify(validate);
    form.render();
    initFormValue();
    initDate('');
    initTime();
    initRealName();

    function initDate(id) {
        // console.log("初始化日期schedulingDate" + id)
        //初始化日期控件
        $view.find('input[name=schedulingDate]').each(function () {
            let dId = $(this).attr("id");
            let fId = dId.substring(14,dId.length);
            laydate.render({
                elem: '#schedulingDate' + fId,
                trigger: 'click',
                btns:['now','confirm']
            })
        });
    }

    function initTime(){
        $view.find('input[name=schedulingTime]').each(function () {
            let dId = $(this).attr("id");
            let fId = dId.substring(14,dId.length);
            laydate.render({
                elem: '#schedulingTime' + fId,
                trigger: 'click',
                range:true,
                type:'time',
                format:'HH:mm',
                btns:['now','confirm']
            })
        });
    }

    function initFormValue() {
        if (baseArea) {
            form.val("baseAreaScheduling-edit-form", {
                "areaId": baseArea.areaId,
                "tenantId": baseArea.tenantId,
            });
            // console.log("编辑排版日期" + JSON.stringify(baseAreaScheduling))
            if (baseAreaScheduling) {
                var count = 0;
                let schedulingDate;
                let i=0;
                baseAreaScheduling.forEach(item => {
                    if(i == 0){
                        $view.find("#schedulingDate0").val(item.schedulingDate);
                        $view.find("#schedulingName00").val(item.schedulingName);
                        $view.find("#schedulingTime00").val(item.schedulingTime);
                        $view.find("#userId00").val(item.userId);
                        $view.find("#userNo00").val(item.userNo);
                        $view.find("#userName00").val(item.userName);
                        $view.find("#realName00").val(item.realName);
                        schedulingDate = item.schedulingDate;
                        if(item.dateFlg == 0){
                            $view.find("#schedulingName00").attr('disabled','disabled');
                            $view.find("#schedulingDate0").attr('disabled','disabled');
                            $view.find("#schedulingName00").attr('disabled','disabled');
                            $view.find("#schedulingTime00").attr('disabled','disabled');
                            $view.find("#userId00").attr('disabled','disabled');
                            $view.find("#userNo00").attr('disabled','disabled');
                            $view.find("#userName00").attr('disabled','disabled');
                            $view.find("#realName00").attr('disabled','disabled');
                            $view.find("#addScheduling0").remove();
                            $view.find("#addBtn00").remove();
                        }
                    }else{
                        if(schedulingDate == item.schedulingDate){
                            let index = count.toString()+i.toString();
                            let schedulingDiv = '<div class="layui-form-item"><div class="layui-col-md4"> ' +
                                '                                <label class="layui-form-label  febs-form-item-require">班次名称：</label> ' +
                                '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
                                '                                    <input type="text" name="schedulingName" id="schedulingName'+index+'" ' ;
                            if(item.dateFlg == 0){
                                schedulingDiv += ' disabled ';
                            }
                            schedulingDiv += ' placeholder="请选择排班名称" autocomplete="off" class="layui-input"  value="'+item.schedulingName+'" > ' +
                                '                                </div> ' +
                                '                            </div> ' +
                                '                            <div class="layui-col-md4"> ' +
                                '                                <label class="layui-form-label  febs-form-item-require">班次时间：</label> ' +
                                '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
                                '                                    <input type="text" name="schedulingTime" id="schedulingTime'+index+'" ' ;
                            if(item.dateFlg == 0){
                                schedulingDiv += ' disabled ';
                            }
                            schedulingDiv +=    '                                           placeholder="请选择排班时间" autocomplete="off" class="layui-input"  value="'+item.schedulingTime+'"> ' +
                                '                                </div> ' +
                                '                            </div> ' +
                                '                            <div class="layui-col-md4"> ' +
                                '                                <label class="layui-form-label  febs-form-item-require">负责人：</label> ' +
                                '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
                                '                                    <input type="text" name="userId" id="userId'+index+'" class="febs-hide" value="'+item.userId+'"> ' +
                                '                                    <input type="text" name="userNo" id="userNo'+index+'" class="febs-hide" value="'+item.userNo+'"> ' +
                                '                                    <input type="text" name="userName" id="userName'+index+'" class="febs-hide " value="'+item.userName+'"> ' +
                                '                                    <input type="text" name="realName" id="realName'+index+'" autocomplete="off" ' +
                                '                                           class="layui-input " lay-verify="required" ';
                            if(item.dateFlg == 0){
                                schedulingDiv += ' disabled ';
                            }
                            schedulingDiv += '                                           minlength="2" maxlength="10" placeholder="请选择人员" value="'+item.realName+'"> ';
                                if(item.dateFlg == 1){
                                    schedulingDiv += '                                    <div class="layui-input-suffix" name="addBtn" id="addBtn'+index+'"> ' +
                                        '                                        <i class="layui-icon layui-icon-add-1"></i> ' +
                                        '                                    </div> ';
                                }
                            schedulingDiv += '                                </div> ' +
                                '                            </div>';


                            if(item.dateFlg == 1){
                                    schedulingDiv  += '          <div name="deleteDetail" id="delDetailBtn0" class="layui-btn layui-btn-sm layui-btn-danger" style="margin: 5px 5px 10px 10px;">' +
                                        '                                <em class="layui-icon">&#xe640;</em>' +
                                        '                            </div>';
                                }
                                schedulingDiv  += '</div>';
                            $("#schedulingDiv"+count).append(schedulingDiv);

                        }else{
                            panelCount ++;
                            count ++;
                            let workPanel = '<div class="layui-card"> ';
                                if(item.dateFlg == 1){
                                    workPanel += '                       <div class="layui-row layui-row-button" style="float: right">' +
                                        '                        <div name="delete" class="layui-btn layui-btn-sm layui-btn-danger">' +
                                        '                            <em class="layui-icon layui-icon-delete"></em> 删除信息' +
                                        '                        </div>' +
                                        '                       </div>';
                                }
                            workPanel += '                    <div class="layui-card-header"> ' +
                                '                        <div class="layui-form-item" style="margin-bottom: 0px;"> ' +
                                '                            <div class="layui-col-md4"> ' +
                                '                                <label class="layui-form-label ">班次日期：</label> ' +
                                '                                <div class="layui-input-block"> ' +
                                '                                    <input type="text" name="schedulingDate" id="schedulingDate'+count+'" ' ;
                            if(item.dateFlg == 0){
                                workPanel += ' disabled ';
                            }
                            workPanel += '                                           placeholder="请选择排班日期" autocomplete="off" class="layui-input" value="'+item.schedulingDate+'"> ' +
                                '                                </div> ' +
                                '                            </div> ' ;
                                if(item.dateFlg == 1){
                                    workPanel += '                            <div name="addScheduling" id="addScheduling'+count+'" class="layui-btn layui-btn-sm" style="margin: 5px 5px 10px 10px;"> ' +
                                        '                                <em class="layui-icon">&#xe654;</em> ' +
                                        '                            </div> ';
                                }
                            workPanel += '                        </div> ' +
                                '                    </div> ' +
                                '                    <div class="layui-card-body"> ' +
                                '                        <div class="layui-row" id="schedulingDiv'+count+'"> ' +
                                '                           <div class="layui-form-item">'+
                                '                            <div class="layui-col-md4"> ' +
                                '                                <label class="layui-form-label  febs-form-item-require">班次名称：</label> ' +
                                '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
                                '                                    <input type="text" name="schedulingName" id="schedulingName'+count+'0" ' ;
                            if(item.dateFlg == 0){
                                workPanel += ' disabled ';
                            }
                            workPanel += '                                           placeholder="请选择排班名称" autocomplete="off" class="layui-input"  value="'+item.schedulingName+'"> ' +
                                '                                </div> ' +
                                '                            </div> ' +
                                '                            <div class="layui-col-md4"> ' +
                                '                                <label class="layui-form-label  febs-form-item-require">班次时间：</label> ' +
                                '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
                                '                                    <input type="text" name="schedulingTime" id="schedulingTime'+count+'0" ' ;
                            if(item.dateFlg == 0){
                                workPanel += ' disabled ';
                            }
                            workPanel += '                                           placeholder="请选择排班时间" autocomplete="off" class="layui-input" value="'+item.schedulingTime+'"> ' +
                                '                                </div> ' +
                                '                            </div> ' +
                                '                            <div class="layui-col-md4"> ' +
                                '                                <label class="layui-form-label  febs-form-item-require">负责人：</label> ' +
                                '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
                                '                                    <input type="text" name="userId" id="userId'+count+'0" class="febs-hide" value="'+item.userId+'"> ' +
                                '                                    <input type="text" name="userNo" id="userNo'+count+'0" class="febs-hide" value="'+item.userNo+'"> ' +
                                '                                    <input type="text" name="userName" id="userName'+count+'0" class="febs-hide " value="'+item.userName+'"> ' +
                                '                                    <input type="text" name="realName" id="realName'+count+'0" autocomplete="off" ' ;
                            if(item.dateFlg == 0){
                                workPanel += ' disabled ';
                            }
                            workPanel +=   '                                           class="layui-input " lay-verify="required" ' +
                                '                                           minlength="2" maxlength="10" placeholder="请选择人员" value="'+item.realName+'"> ' ;
                                if(item.dateFlg==1){
                                    workPanel += '                                    <div class="layui-input-suffix" name="addBtn" id="addBtn'+count+'0"> ' +
                                        '                                        <i class="layui-icon layui-icon-add-1"></i> ' +
                                        '                                    </div> ';
                                }
                            workPanel += '                                </div> ' +
                                '                            </div> ' +
                                '                            </div> ' +
                                '                        </div> ' +
                                '                    </div> ' +
                                '                </div>';

                            $("#workInfo").append(workPanel);
                        }
                        schedulingDate = item.schedulingDate;
                    }
                    i++;
                })
                let buttonHtml = '<div class="layui-row">'+
                    '             <div class="layui-form-item layui-row-button" style="float: right">' +
                    '                <div class="layui-inline" style="text-align: right">' +
                    '                    <div id="addWork" class="layui-btn layui-btn-sm">' +
                    '                        <em class="layui-icon">&#xe654;</em> 增加排班信息' +
                    '                    </div>' +
                    '                </div>' +
                    '            </div>'+
                    '</div>';
                $("#workInfoSet").append(buttonHtml);
            }
        } else {
            let buttonHtml = '<div class="layui-row">'+
                '             <div class="layui-form-item layui-row-button" style="float: right">' +
                '                <div class="layui-inline" style="text-align: right">' +
                '                    <div id="addWork" class="layui-btn layui-btn-sm">' +
                '                        <em class="layui-icon">&#xe654;</em> 增加排班信息' +
                '                    </div>' +
                '                </div>' +
                '            </div>'+
                '</div>';
            $("#workInfoSet").append(buttonHtml);
            form.val("baseAreaScheduling-edit-form", {
                "tenantId": POPUP_DATA.tenantId
            });

        }
        form.render();
    }

    form.on('submit(baseAreaScheduling-edit-form-submit)', function (data) {
        var schedulingList = [];
        $(".layui-card").each(function (index, item) {
            if(index==0){
                return true;
            }
            var count = index -1;
            var date = $('#schedulingDate'+count).val();
            var container = $(".layui-card-body").eq(index).find('.layui-form-item');
            $.each(container, function (index, item) {
                var userId = container.eq(index).find("input[name=userId]").val();
                var userNo = container.eq(index).find("input[name=userNo]").val();
                var userName = container.eq(index).find("input[name=userName]").val();
                var realName = container.eq(index).find("input[name=realName]").val();
                var schedulingName = container.eq(index).find("input[name=schedulingName]").val();
                var schedulingTime = container.eq(index).find("input[name=schedulingTime]").val();
                schedulingList.push({
                    userId: userId,
                    userNo: userNo,
                    userName: userName,
                    realName: realName,
                    schedulingDate: date,
                    schedulingName:schedulingName,
                    schedulingTime:schedulingTime
                })
            })
        })
        data.field.schedulingList = schedulingList;
        if(baseArea){
            data.field.areaId = baseArea.areaId;
        }

        var updateData = data.field;
        // console.log("保存提交信息" + JSON.stringify(updateData));
        addOrUpdate(updateData);
        return false;
    });

    // layui table select 事件
    form.on('select(areaId)', function (obj) {
        // 获取选中项 value
        var areaId = obj.value;
        if (areaId == '') {
            $('#sort').text('--');
            $('#areaType').text('--');
            $('#status').text('--');
        } else {
            areaList.forEach(item => {
                if (item.areaId == areaId) {
                    // console.log("选中区域" + JSON.stringify(item));
                    $('#sort').text(item.sort);
                    $('#tenantId').val(item.tenantId);
                    if (item.areaType == '1') {
                        $('#areaType').text('内场区域');
                    } else {
                        $('#areaType').text('项目区域');
                    }
                    if (item.status == '1') {
                        $('div[id="status"]').text('启用');
                    } else {
                        $('div[id="status"]').text('禁用');
                    }
                }
            })
        }

        form.render();
    });

    function addOrUpdate(data) {
        febs.modal.confirm('保存提示', '是否保存排班信息？', function () {
            if (baseArea) {
                febs.postArray(ctx + 'baseAreaScheduling/update', data, function () {
                    layer.closeAll();
                    febs.alert.success('修改成功');
                    $('#febs-baseAreaScheduling').find('#query').trigger('click');
                });
            } else {
                febs.postArray(ctx + 'baseAreaScheduling', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-baseAreaScheduling').find('#query').trigger('click');
                });
            }
        });


    }


    $(document).off('click', 'div[name="addBtn"]').on('click', 'div[name="addBtn"]', function (e) {
        // console.log("选择用户租户阿红" + $('#tenantId').val() + 'sss')
        febs.modal.open('选择用户', 'common/user', {
            btn: ['确认', '关闭'],
            area: $(window).width() <= 750 ? ['95%', '85%'] : ['50%', '85%'],
            data: {
                tenantId: $('#tenantId').val(),
                multiple: false,
            },
            yes: function (index, layero) {
                let selectData = null
                if (typeof getSelectData === 'function') {
                    selectData = getSelectData()
                }
                temp = selectData;
                // console.log("获得的人员" + JSON.stringify(temp))

                if (selectData) {
                    // var children = e.target.parentElement.childNodes;
                    var children = $(e.target).parentsUntil('div.layui-col-md4').children();
                    for (var i = 0; i < children.length; i++) {
                        if (children[i].name == 'userId') {
                            children[i].value = temp.userId
                        } else if (children[i].name == 'userNo') {
                            children[i].value = temp.userNo
                        }else if (children[i].name == 'userName') {
                            children[i].value = temp.userName
                        } else if (children[i].name == 'realName') {
                            children[i].value = temp.uname
                        }
                    }
                    // reloadTable(transferDta(workUser,temp));
                    layer.close(index);
                }

            },
            btn2: function (index, layero) {
                layer.close(index);
                return false;
            }
        });

    });

    $("#addWork").on('click', function () {
        var workPanel = '<div class="layui-card"> ' +
            '                    <div class="layui-card-header"> ' +
            '                        <div class="layui-form-item" style="margin-bottom: 0px;"> ' +
            '                            <div class="layui-col-md4"> ' +
            '                                <label class="layui-form-label ">班次日期：</label> ' +
            '                                <div class="layui-input-block"> ' +
            '                                    <input type="text" name="schedulingDate" id="schedulingDate'+panelCount+'" ' +
            '                                           placeholder="请选择排班日期" autocomplete="off" class="layui-input"> ' +
            '                                </div> ' +
            '                            </div> ' +
            '                            <div name="addScheduling" id="addScheduling'+panelCount+'" class="layui-btn layui-btn-sm" style="margin: 5px 5px 10px 10px;"> ' +
            '                                <em class="layui-icon">&#xe654;</em> ' +
            '                            </div> ' +
            '                               <div name="delete" class="layui-btn layui-btn-sm layui-btn-danger" style="float: right;margin: 5px 5px 10px 10px;">' +
            '                                   <em class="layui-icon layui-icon-delete"></em> 删除信息' +
            '                               </div>' +
            '                        </div> ' +
            '                    </div> ' +
            '                    <div class="layui-card-body"> ' +
            '                        <div class="layui-row" id="schedulingDiv'+panelCount+'"> ' +
            '                           <div class="layui-form-item">'+
            '                            <div class="layui-col-md4"> ' +
            '                                <label class="layui-form-label  febs-form-item-require">班次名称：</label> ' +
            '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
            '                                    <input type="text" name="schedulingName" id="schedulingName'+panelCount+'0" ' +
            '                                           placeholder="请选择排班名称" autocomplete="off" class="layui-input"> ' +
            '                                </div> ' +
            '                            </div> ' +
            '                            <div class="layui-col-md4"> ' +
            '                                <label class="layui-form-label  febs-form-item-require">班次时间：</label> ' +
            '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
            '                                    <input type="text" name="schedulingTime" id="schedulingTime'+panelCount+'0" ' +
            '                                           placeholder="请选择排班时间" autocomplete="off" class="layui-input"> ' +
            '                                </div> ' +
            '                            </div> ' +
            '                            <div class="layui-col-md4"> ' +
            '                                <label class="layui-form-label  febs-form-item-require">负责人：</label> ' +
            '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
            '                                    <input type="text" name="userId" id="userId'+panelCount+'0" class="febs-hide"> ' +
            '                                    <input type="text" name="userNo" id="userNo'+panelCount+'0" class="febs-hide"> ' +
            '                                    <input type="text" name="userName" id="userName'+panelCount+'0" class="febs-hide "> ' +
            '                                    <input type="text" name="realName" id="realName'+panelCount+'0" autocomplete="off" ' +
            '                                           class="layui-input " lay-verify="required" ' +
            '                                           minlength="2" maxlength="10" placeholder="请选择人员"> ' +
            '                                    <div class="layui-input-suffix" name="addBtn" id="addBtn'+panelCount+'0"> ' +
            '                                        <i class="layui-icon layui-icon-add-1"></i> ' +
            '                                    </div> ' +
            '                                </div> ' +
            '                            </div> ' +
            '                            </div> ' +
            '                        </div> ' +
            '                    </div> ' +
            '                </div>';
        
        $("#workInfo").append(workPanel);
        initDate(panelCount);
        initTime();
        initRealName();
        form.render();
        panelCount++;
    });

    $(document).off('click', 'div[name=addScheduling]').on('click','div[name=addScheduling]',function(){
        let id = $(this).attr("id");
        let num = id.substring(13,id.length);
        let count = $("#schedulingDiv"+num).find(".layui-form-item").length;
        let index = num+count;
        let schedulingDiv = '<div class="layui-form-item"><div class="layui-col-md4"> ' +
            '                                <label class="layui-form-label  febs-form-item-require">班次名称：</label> ' +
            '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
            '                                    <input type="text" name="schedulingName" id="schedulingName'+index+'" ' +
            '                                           placeholder="请选择排班名称" autocomplete="off" class="layui-input"> ' +
            '                                </div> ' +
            '                            </div> ' +
            '                            <div class="layui-col-md4"> ' +
            '                                <label class="layui-form-label  febs-form-item-require">班次时间：</label> ' +
            '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
            '                                    <input type="text" name="schedulingTime" id="schedulingTime'+index+'" ' +
            '                                           placeholder="请选择排班时间" autocomplete="off" class="layui-input"> ' +
            '                                </div> ' +
            '                            </div> ' +
            '                            <div class="layui-col-md4"> ' +
            '                                <label class="layui-form-label  febs-form-item-require">负责人：</label> ' +
            '                                <div class="layui-input-block layui-input-icon-container" style="position: relative"> ' +
            '                                    <input type="text" name="userId" id="userId'+index+'" class="febs-hide"> ' +
            '                                    <input type="text" name="userNo" id="userNo'+index+'" class="febs-hide"> ' +
            '                                    <input type="text" name="userName" id="userName'+index+'" class="febs-hide "> ' +
            '                                    <input type="text" name="realName" id="realName'+index+'" autocomplete="off" ' +
            '                                           class="layui-input " lay-verify="required" ' +
            '                                           minlength="2" maxlength="10" placeholder="请选择人员"> ' +
            '                                    <div class="layui-input-suffix" name="addBtn" id="addBtn'+index+'"> ' +
            '                                        <i class="layui-icon layui-icon-add-1"></i> ' +
            '                                    </div> ' +
            '                                </div> ' +
            '                            </div>'+
            '                           <div name="deleteDetail" id="delDetailBtn0" class="layui-btn layui-btn-sm layui-btn-danger" style="margin: 5px 5px 10px 10px;">' +
            '                                <em class="layui-icon">&#xe640;</em>' +
            '                            </div></div>';
        $("#schedulingDiv"+num).append(schedulingDiv);
        initTime();
        initRealName();
        form.render();
    })

    $(document).off('click', 'div[name=delete]').on('click', 'div[name=delete]', function (e) {
        // console.log("删除元素")
        $(this).parents('#workInfoSet .layui-card').remove();
        form.render();
    });

    $(document).off('click', 'div[name=deleteDetail]').on('click', 'div[name=deleteDetail]', function (e) {
        // console.log("删除元素")
        $(this).parents('div.layui-form-item').remove();
        form.render();
    });

    function initRealName(){
        $view.find('input[name="realName"]').autocomplete({
            serviceUrl: ctx + 'common/user/list',//测试接口
            paramName:'keywords',
            useType:'user',
            params:{ tenantId: POPUP_DATA.tenantId},
            onSelect: function(suggestion) {
                let num = $(this).attr("id").substring(8,$(this).attr("id").length);
                $view.find('#realName'+num+'').val(suggestion.value);
                $view.find('#userId'+num+'').val(suggestion.data)
                $view.find('#userNo'+num+'').val(suggestion.userNo)
                $view.find('#userName'+num+'').val(suggestion.userName);
            },
        });
    }

});